package cn.hh.order.service.impl;


import cn.hh.basic.service.impl.BasicServiceImpl;
import cn.hh.basic.util.CodeGenerateUtils;
import cn.hh.order.domain.AdoptOrder;
import cn.hh.order.domain.OrderAddress;
import cn.hh.order.mapper.AdoptOrderMapper;
import cn.hh.order.mapper.OrderAddressMapper;
import cn.hh.order.service.IAdoptOrderService;
import cn.hh.pay.constants.OrderConstants;
import cn.hh.pay.domain.PayBill;
import cn.hh.pay.mapper.PayBillMapper;
import cn.hh.pay.service.IPayBillService;
import cn.hh.pet.domain.Pet;
import cn.hh.pet.mapper.PetMapper;
import cn.hh.user.domain.User;
import cn.hh.user.domain.UserAddress;
import cn.hh.user.mapper.UserAddressMapper;
import cn.hh.user.mapper.UserMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.Map;

@Service
public class AdoptOrderServiceImpl extends BasicServiceImpl<AdoptOrder> implements IAdoptOrderService {
    @Autowired
    private AdoptOrderMapper adoptOrderMapper;
    @Autowired
    private PetMapper petMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserAddressMapper userAddressMapper;
    @Autowired
    private OrderAddressMapper orderAddressMapper;
    @Autowired
    private PayBillMapper payBillMapper;
    @Autowired
    private IPayBillService payBillService;

    @Override
    public String submit(Map<String, Object> map, Long loginId) {
        // 1.获取传回来的数据
        /*宠物id*/
        Long petId = Long.valueOf(map.get("pet_id").toString());
        /*用户地址id*/
        Long addressId = Long.valueOf(map.get("address_id").toString());
        /*物流选择*/
        Integer logisticsType = Integer.valueOf(map.get("logisticsType").toString());
        /*支付方式*/
        Integer payType = Integer.valueOf(map.get("payType").toString());
        // 2.改变该宠物的上下架状态
        /*获取宠物信息*/
        Pet pet = petMapper.loadById(petId);
        /*h获取当前用户id*/
        User user = userMapper.loadByLoginId(loginId);
        /*判断宠物是否存在*/
        if (pet!=null){
            /*修改宠物为下架状态*/
            pet.setState(0);
            /*下架时间*/
            pet.setOffsaletime(new Date());
            /*设置用户id*/
            pet.setUser_id(user.getId());
            /*修改*/
            petMapper.update(pet);
        }
        // 3.生成订单
        /*通过宠物信息和用户信息，录入订单*/
        AdoptOrder adoptOrder =order2adoptOrder(addressId,pet,user);
        String unionPay=CodeGenerateUtils.generateUnionPaySn();
        adoptOrder.setPaySn(unionPay);
        adoptOrderMapper.add(adoptOrder);
        // 4.生成地址快照
        /*获取用户地址*/
        UserAddress userAddress = userAddressMapper.loadById(addressId);
        OrderAddress orderAddress = adoptOrder2orderAddress(adoptOrder,userAddress);
        orderAddressMapper.add(orderAddress);
        // 5.支付
        // 5.1生成支付订单
        PayBill payBill = getPayBill(payType, pet, user, adoptOrder, unionPay);
        /*保存支付订单*/
        payBillMapper.add(payBill);
        // 5.2调用封装的支付接口返回同步信息给前端(扫码界面)
        // 6.添加定时器

        return payBillService.pay(payBill);
    }

    private PayBill getPayBill(Integer payType, Pet pet, User user, AdoptOrder adoptOrder, String unionPay) {
        PayBill payBill = new PayBill();
        /*设置交易摘要*/
        payBill.setDigest(adoptOrder.getDigest());
        /*设置金额*/
        payBill.setMoney(adoptOrder.getPrice());
        /*设置交易编号*/
        payBill.setUnionPaySn(unionPay);
        /*设置过期时间*/
        payBill.setLastPayTime(new Date(System.currentTimeMillis()+ OrderConstants.ORDER_TIMEOUT));
        /*设置支付方式*/
        payBill.setPayChannel(payType);
        /*设置交易类型,属于哪种订单*/
        payBill.setBusinessType(OrderConstants.BUSINESS_TYPE_ADOPT_ORDER);
        /*设置交易编号*/
        payBill.setBusinessKey(adoptOrder.getId());
        /*设置用户名*/
        payBill.setNickName(user.getUsername());
        /*设置用户id*/
        payBill.setUser_id(user.getId());
        /*设置店铺名称*/
        payBill.setShop_id(pet.getShop_id());
        /*设置状态*/
        payBill.setState(0);
        return  payBill;
    }

    private OrderAddress adoptOrder2orderAddress(AdoptOrder adoptOrder, UserAddress userAddress) {
        OrderAddress orderAddress = new OrderAddress();
        /*copy*/
        BeanUtils.copyProperties(userAddress, orderAddress);
        /*因为拷贝会拷贝id，所以要设置为空*/
        orderAddress.setId(null);
        orderAddress.setOrder_id(adoptOrder.getId());
        orderAddress.setOrderSn(adoptOrder.getOrderSn());
        return orderAddress;
    }

    private AdoptOrder order2adoptOrder(Long addressId, Pet pet, User user) {
        AdoptOrder adoptOrder=new AdoptOrder();
        adoptOrder.setDigest("[提示]"+pet.getName()+"的领养订单");
        adoptOrder.setPrice(pet.getSaleprice());
        adoptOrder.setOrderSn(CodeGenerateUtils.generateOrderSn(user.getId()));
        /*设置最后时间为下单时间加十五分钟*/
        adoptOrder.setLastConfirmTime(new Date(System.currentTimeMillis()+OrderConstants.ORDER_TIMEOUT));
        adoptOrder.setPet_id(pet.getId());
        adoptOrder.setUser_id(user.getId());
        adoptOrder.setAddress_id(addressId);
        return adoptOrder;
    }
}
